from elasticsearch import Elasticsearch

es = Elasticsearch('http://192.168.8.182:9200/')


def add_to_index(index, model):
    payload = {}
    for field in model.__searchable__:
        payload[field] = getattr(model, field)
    es.index(index=index, doc_type=index, id=model.id, body=payload)


def query_index(index, query):
    search = es.search(
        index=index, doc_type=index,
        body={"query": {'match': {'nickname': query}}})
    ids = [int(hit['_id']) for hit in search['hits']['hits']]
    return ids, search['hits']

